package com.dys.testlocaldatetime06;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.TemporalAccessor;
import java.util.Locale;

/**
 * @className: com.dys.testlocaldatetime06.LocalDateTimeTest
 * @description: com.dys.testlocaldatetime06
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/07/24 下午11:00
 */
public class LocalDateTimeTest {
    /**
     * 这是一个main()方法,是应用程序的入口
     *
     * @param args 方法的形参列表,是一个字符串数组,包含了从命令行传递给程序的参数
     */
    public static void main(String[] args) {
        new LocalDateTimeTest().testDateTimeFormatter2();
    }

    public void testLocalDateTime2() {
        // 1.完成实例化
        // now()-->获取当前的日期
        LocalDate now = LocalDate.now();
        System.out.println(now);
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime);
        LocalTime now1 = LocalTime.now();
        System.out.println(now1);

        // 2.方法：of()-->指定的日期，时间：日期+时间
        LocalDate localDate = LocalDate.of(2024, 7, 24);
        System.out.println(localDate);
        LocalTime localTime = LocalTime.of(12, 34, 56);
        System.out.println(localTime);
        LocalDateTime localDateTime1 = LocalDateTime.of(1999, 10, 25, 12, 23, 34);
        System.out.println(localDateTime1);

        // get()方法
        int year = localDateTime1.getYear();
        System.out.println(year);
        int month = localDateTime1.getMonthValue();
        System.out.println(month);
        Month month1 = localDateTime1.getMonth();
        System.out.println(month1);
        DayOfWeek day = localDateTime1.getDayOfWeek();
        System.out.println(day);
        int dayOfMonth = localDateTime1.getDayOfMonth();
        System.out.println(dayOfMonth);
        int hour = localDateTime1.getHour();
        System.out.println(hour);
        int minute = localDateTime1.getMinute();
        System.out.println(minute);
        int second = localDateTime1.getSecond();
        System.out.println(second);

        // 不是set方法，叫with
        // 体会：不可变性
        LocalDateTime localDateTime2 = localDateTime1.withMonth(12);
        System.out.println(localDateTime2);
        System.out.println(localDateTime1);

        // 提供了加减的操作：
        // 加：
        LocalDateTime localDateTime3 = localDateTime1.plusMonths(2);
        System.out.println(localDateTime3);

        // 减：
        LocalDateTime localDateTime4 = localDateTime1.minusMonths(10);
        System.out.println(localDateTime4);
    }

    public void testLocalDateTime() {
        // 1.完成实例化：
        // 方法1：now()--获取当前的日期，时间，日期+时间
        LocalDate localDate = LocalDate.now();
        System.out.println(localDate);
        LocalTime localTime = LocalTime.now();
        System.out.println(localTime);
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime);
        // 方法2：of()--设置指定的日期，时间，日期+时间
        LocalDate of = LocalDate.of(2010, 5, 6);
        System.out.println(of);
        LocalTime of1 = LocalTime.of(12, 35, 56);
        System.out.println(of1);
        LocalDateTime of2 = LocalDateTime.of(1890, 12, 23, 13, 24, 15);
        System.out.println(of2);
        // LocalDate,LocalTime用的不如LocalDateTime多
        // 下面讲解用LocalDateTime：
        // 一些列常用的get***
        System.out.println(localDateTime.getYear());// 2020
        System.out.println(localDateTime.getMonth());// JUNE
        System.out.println(localDateTime.getMonthValue());// 6
        System.out.println(localDateTime.getDayOfMonth());// 14
        System.out.println(localDateTime.getDayOfWeek());// SUNDAY
        System.out.println(localDateTime.getHour());// 22
        System.out.println(localDateTime.getMinute());// 22
        System.out.println(localDateTime.getSecond());// 6
        // 不是set方法，叫with
        // 体会：不可变性
        LocalDateTime localDateTime2 = localDateTime.withMonth(8);
        System.out.println(localDateTime);
        System.out.println(localDateTime2);
        // 提供了加减的操作：
        // 加：
        LocalDateTime localDateTime1 = localDateTime.plusMonths(4);
        System.out.println(localDateTime);
        System.out.println(localDateTime1);
        // 减：
        LocalDateTime localDateTime3 = localDateTime.minusMonths(5);
        System.out.println(localDateTime);
        System.out.println(localDateTime3);

    }

    public void testDateTimeFormatter2() {
        DateTimeFormatter df1 = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        // df1就可以帮我们完成LocalDateTime和String之间的相互转换：
        // LocalDateTime-->String:
        LocalDateTime localDateTime = LocalDateTime.now();
        String format = df1.format(localDateTime);
        System.out.println(format);
        // String--->LocalDateTime
        TemporalAccessor parse = df1.parse("2024-07-25T21:48:21.558389");
        System.out.println(parse);
        // 方式二:本地化相关的格式
        DateTimeFormatter df2 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG).withLocale(Locale.CHINA);
        // LocalDateTime-->String:
        ZonedDateTime now = ZonedDateTime.now();
        String format2 = df2.format(now);
        System.out.println(format2);
        // String--->LocalDateTime
        // DateTimeFormatter df23 =new DateTimeFormatterBuilder().appendPattern("yy-M-d ahh:mm").toFormatter(Locale.CHINA);
        // TemporalAccessor parse1 = df23.parse("20-6-15 下午3:18");
        // System.out.println(parse1);
        // 方式三: 自定义的格式。如: ofPattern( "yyyy-MM-dd hh:mm:ss") ---》重点，以后常用
        DateTimeFormatter df3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        // LocalDateTime-->String:
        LocalDateTime now2 = LocalDateTime.now();
        String format1 = df3.format(now2);
        System.out.println(format1);
        // String--->LocalDateTime
        TemporalAccessor parse1 = df3.parse("2020-06-15 03:22:03");
        System.out.println(parse1);
    }

    public void testDateTimeFormatter() {
        // 格式化类：DateTimeFormatter
        // 方式一:预定义的标准格式。如: ISO_LOCAL_DATE_TIME;ISO_LOCAL_DATE;IS0_LOCAL_TIME
        DateTimeFormatter df1 = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        // df1就可以帮我们完成LocalDateTime和String之间的相互转换：
        // LocalDateTime-->String:
        LocalDateTime now = LocalDateTime.now();
        String str = df1.format(now);
        System.out.println(str);// 2020-06-15T15:02:51.29
        // String--->LocalDateTime
        TemporalAccessor parse = df1.parse("2020-06-15T15:02:51.29");
        System.out.println(parse);
        // 方式二:本地化相关的格式。如: oflocalizedDateTime()
        // 参数：FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT
        // FormatStyle.LONG :2020年6月15日 下午03时17分13秒
        // FormatStyle.MEDIUM: 2020-6-15 15:17:42
        // FormatStyle.SHORT:20-6-15 下午3:18
        DateTimeFormatter df2 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
        // LocalDateTime-->String:
        // LocalDateTime now1 = LocalDateTime.now();
        ZonedDateTime now1 = ZonedDateTime.now();
        String str2 = df2.format(now1);
        System.out.println(str2);
        // String--->LocalDateTime
        TemporalAccessor parse1 = df2.parse("20-6-15 下午3:18");
        System.out.println(parse1);
        // 方式三: 自定义的格式。如: ofPattern( "yyyy-MM-dd hh:mm:ss") ---》重点，以后常用
        DateTimeFormatter df3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        // LocalDateTime-->String:
        LocalDateTime now2 = LocalDateTime.now();
        String format = df3.format(now2);
        System.out.println(format);// 2020-06-15 03:22:03
        // String--->LocalDateTime
        TemporalAccessor parse2 = df3.parse("2020-06-15 03:22:03");
        System.out.println(parse2);

    }
}
